<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/8
 * Time: 15:42
 */

namespace backend\controllers;

use backend\models\User;
use common\components\Error;
use common\controllers\BaseController;
use Yii;
use yii\base\ErrorException;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\helpers\Url;

class AdminBaseController extends BaseController
{
    /**
     * @var User
     */
    public $userInfo;
    protected $menus = [];
    /**
     * 编辑页和新增页是否不同
     * @var int
     */
    protected $editDef = 0;

    public $ListField = '';
    public $editField = '';
    public $addField = '';

    public $model = null;
    /**
     * @var \common\services\BasicService
     */
    public $service = null;


    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        Yii::$app->params['title'] = '后台管理';
        $userInfo = Yii::$app->session->get(Yii::$app->session->name);
        if ($userInfo) {
            $this->userInfo = $userInfo;
            Yii::$app->params['userInfo'] = $userInfo;
        }
        if (!Yii::$app->request->isAjax && !$userInfo) {
            //非ajax请求且未登录
            $this->redirect(['/login/index'])->send();
        }
    }

    /**
     * 默认列表页
     * @throws ErrorException
     */
    public function actionIndex()
    {
        $data = Yii::$app->request->post();
        if (ArrayHelper::getValue($data, 'draw')) {
            if ($this->service) {
                $map = [];
                if (method_exists($this, '_filter')) {
                    $this->_filter($map);
                }
                $res = $this->service->getTableData($data);
                return Json::encode($res);
            } else {
                throw new ErrorException('模型不存在');
            }
        }
        return $this->render('index');
    }

    /**
     * 默认编辑页
     * @throws ErrorException
     */
    public function actionEdit()
    {

        if (!is_object($this->service)) {
            throw new ErrorException('模型不存在');
        }
        //ajax请求
        if (Yii::$app->request->isAjax) {
            $resp = Error::tryCatch(function(){
                $data = Yii::$app->request->post();
                $rs = $this->service->save($data);
                if ($rs) {
                    return $this->success('保存成功', [], Url::to(['index']));
                } else {
                    Error::exception('保存失败', Error::SAVE_FAILED);
                }
            });
            return $this->ajaxArray($resp);
        }
        $viewVar = [];
        //获取信息
        if ($id = Yii::$app->request->get('id')) {
            $info = $this->service->getInfoByPk($id);
            $viewVar['info'] = $info;
        }
        if (!isset($viewVar['info'])) {
            $viewVar['info'] = [];
        }
        //获取页面变量
        if (method_exists($this, 'getViewVars')) {
            if ($tmp = $this->getViewVars('edit', $viewVar['info'])) {
                $viewVar = array_merge($viewVar, $tmp);
            }
        }

        return $this->render($this->editDef && !$viewVar['info'] ? 'add' : 'edit', $viewVar);
    }

    /**
     * 默认删除
     * @throws ErrorException
     */
    public function actionDel()
    {
        if (Yii::$app->request->isAjax) {
            $result = Error::tryCatch(function(){
                $id = ArrayHelper::getValue($_REQUEST, 'id');
                $rs = $this->service->del($id);
                if (!$rs) {
                    Error::exception('删除失败', Error::DELETE_FAILED);
                }
                return $this->success('删除成功', [], '', 'common/common-refresh_table');
            });
            return $this->ajaxArray($result);
        }
    }

}